package com.haoxi.gateway.test.message;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.common.TestConfConst;
import com.haoxi.gateway.test.SGIPConstant;
import com.haoxi.gateway.test.util.TypeConvert;

public class SGIPReportMessage extends SGIPMessage {
    private static SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss");

    public SGIPReportMessage(byte[] submitSequenceNumber, int reportType,
            String userNumber, int state, int errorCode)
            throws IllegalArgumentException {
        super.buf = new byte[64];
        TypeConvert.int2byte(64, buf, 0);
        TypeConvert.int2byte(SGIPConstant.Report_Command_Id, buf, 4);
        // 这里就要生成seqID拉
        //System.arraycopy(submitSequenceNumber, 0, buf, 8, 12);
         int icpid = TestConfConst.icpid& 0xffffffff;
         TypeConvert.int2byte(icpid, buf, 8);
         int timeStamp = Integer.parseInt(format.format(new Date()));
         TypeConvert.int2byte(timeStamp, buf, 12);
         setSequenceID();
        System.arraycopy(submitSequenceNumber, 0, buf, 20, 12);
        buf[32] = (byte) reportType;
        System
                .arraycopy(userNumber.getBytes(), 0, buf, 33, userNumber
                        .length());
        buf[54] = (byte) state;
        buf[55] = (byte) errorCode;

    }

    public byte[] getSubmitSequenceNumber() {
        byte temp[] = new byte[12];
        System.arraycopy(buf, 20, temp, 0, 12);
        return temp;
    }

    public String getSubmitSequenceNumberStr() {
        byte temp[] = new byte[12];
        System.arraycopy(buf, 20, temp, 0, 12);
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < temp.length; i++) {
            buff.append(String.valueOf(temp[i]));
        }
        return buff.toString();
    }

    public int getReportType() {
        int tmpId = buf[32];
        return tmpId;
    }

    public String getUserNumber() {
        byte tmpId[] = new byte[21];
        System.arraycopy(buf, 33, tmpId, 0, 21);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public int getState() {
        int tmpId = buf[54];
        return tmpId;
    }

    public int getErrorCode() {
        int tmpId = buf[55];
        return tmpId;
    }

    public String getReserve() {
        byte tmpReserve[] = new byte[8];
        System.arraycopy(buf, 56, tmpReserve, 0, 8);
        return (new String(tmpReserve)).trim();
    }

    public String toString() {
        String tmpStr = "SGIP_REPORT: ";
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nmsgLength=")
                .append(getallLength())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nCommandId=")
                .append(getCommandId())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nSrcNodeId=")
                .append(getSrcNodeId())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nTime_Stamp=")
                .append(getTime_Stamp())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nSequence_Id=")
                .append(getSequenceId())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append(
                "\r\nSubmitSequenceNumber=").append(
                getSubmitSequenceNumberStr())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nReportType=")
                .append(getReportType())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nUserNumber=")
                .append(getUserNumber())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nState=").append(
                getState())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nErrorCode=")
                .append(getErrorCode())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nReserve=")
                .append(getReserve())));
        return tmpStr;
    }

    public int getCommandId() {
        return TypeConvert.byte2int(buf, 4);
    }

    public int getSrcNodeId() {
        return TypeConvert.byte2int(buf, 8);
    }

    public int getTime_Stamp() {
        return TypeConvert.byte2int(buf, 12);
    }

    public int getSequence_Id() {
        return TypeConvert.byte2int(buf, 16);
    }

    public String getSeqStr() {
        return "" + getSrcNodeId() + getTime_Stamp() + getSequence_Id();
    }

    public int getallLength() {
        return TypeConvert.byte2int(buf, 0);
    }
}
